package cc.dd.marketing.coupon.parser;

import cc.dd.marketing.api.constant.FunctionCodeEnum;
import cc.dd.marketing.api.request.coupon.CouponUseReqDTO;
import cc.dd.marketing.api.response.base.BaseResponse;
import cc.dd.marketing.api.response.coupon.CouponUseRespDTO;
import cc.dd.marketing.common.ContextParam;
import cc.dd.marketing.core.AbstractRequestDTOParser;
import cc.dd.marketing.core.annotation.CouponTemplateMapper;
import cc.dd.marketing.core.annotation.FunctionMapper;
import cc.dd.marketing.core.base.ResponseParser;
import cc.dd.marketing.core.constant.CouponTemplateEnum;
import cc.dd.marketing.entity.CouponTemplate;
import cc.dd.marketing.rule.base.Rule;
import cc.dd.marketing.coupon.dto.CouponUseInnerReqDTO;
import cc.dd.marketing.coupon.rule.domain.CouponEndTimeRule;
import cc.dd.marketing.coupon.rule.domain.CouponStartTimeRule;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang3.time.DateUtils;
import org.springframework.stereotype.Component;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 * 优惠券组装
 *
 * @author dongsufeng
 * @date 2022/9/9 2:29 PM
 */
@Component
@CouponTemplateMapper({CouponTemplateEnum.DEFAULT})
@FunctionMapper({FunctionCodeEnum.FREEZE, FunctionCodeEnum.UNFREEZE, FunctionCodeEnum.USE})
@Slf4j
public class CouponOperationParser extends AbstractRequestDTOParser<CouponUseReqDTO, CouponUseInnerReqDTO> implements ResponseParser<CouponUseReqDTO, CouponUseInnerReqDTO> {


    protected CouponUseInnerReqDTO assembleDTO(CouponUseReqDTO request,CouponUseInnerReqDTO innerDTO) {
        if (innerDTO == null){
            innerDTO = new CouponUseInnerReqDTO();
        }
        CouponTemplate couponTemplate = new CouponTemplate();
        couponTemplate.setExpireTime(DateUtils.addDays(new Date(),1));
        couponTemplate.setEffectiveTime(DateUtils.addDays(new Date(),-1));
        innerDTO.setCouponTemplate(couponTemplate);
        return innerDTO;
    }
    @Override
    protected List<Rule> buildRules(CouponUseInnerReqDTO innerDTO) {
        log.info("coupon do buildRules");
        List<Rule> rules = new ArrayList<>();
        rules.add(new CouponStartTimeRule(innerDTO.getCouponTemplate().getEffectiveTime()));
        rules.add(new CouponEndTimeRule(innerDTO.getCouponTemplate().getExpireTime()));
        return rules;
    }


    @Override
    public BaseResponse buildResponse(ContextParam<CouponUseReqDTO, CouponUseInnerReqDTO> contextParam) {
        log.info("coupon do buildResponse");
        CouponUseRespDTO respDTO = new CouponUseRespDTO();
        respDTO.setCouponNo("1111111111");
        return respDTO;
    }
}
